var express = require("express");
var router = express.Router();
let upload = require("../utils/multer");

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});
router.get("/test", (req, res, next) => {
  console.log("该接口正在被调用");
  //http 1.0
  // res.setHeader("Expires",new Date(Date.now()+30000));
  //http 1.1
  //res.setHeader("Cache-Control","max-age=120");
  res.setHeader("Cache-Control", "no-cache");
  let targetDate = new Date("2023-5-20");
  if (new Date(req.headers.ifmodifiedsince) <= targetDate) {
    //没过期 一直用缓存的数据
    res.json({
      code: 1,
      msg: "1.txt",
    });
  } else {
    res.setHeader("Last-Modified", new Date(Date.now()));
    res.json({
      code: 1,
      msg: "2.txt",
    });
  }
});
router.post("/uploads", upload.array("filename"), (req, res) => {
  //如果图片上传成功  返回图片上传的地址
  res.json({
    code: 200,
    url: "/uploads/" + req.files[0].filename,
  });
});

module.exports = router;
